home *** CD-ROM | disk | FTP | other *** search
- //:: program: ps.c
- //:: compile: tcc -mt -lt ps.c
- //:: creator: d.reece
- //:: purpose: print simple text files in postscript.
- //:: written: 1.00 ... 01-12-94
- //:: revised: 1.01 ... 01-14-94 ... ensured fclose()
- //:: ... added duplexing.
- //:: 1.02 ... 01-15-94 ... avoid languagelevel 2 except for duplex.
- //:: ... added dbf/mdx info.
- //:: ... pitch page number if only one page.
- //:: 1.03 ... 01-16-93 ... added time stamp.
- //:: 1.04 ... 01-18-93 ... ability to specify beginning page number.
- //:: 1.05 ... 05-03-94 ... removed dbf stuff.
-
- #include <bios.h>
- #include <time.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
-
- #define VERSION "ps v1.05"
-
- void help( void );
- void bail( char *cMessage, int rc );
- void padBS( char *s );
- int printOK( void );
- int writePP( char *filename, int pt, int pd, int pb );
- int writePS( char *filename, int pt );
- int writePT( int pd );
-
- int main( int argc, char *argv[] )
- {
- int rc;
- int pd = 0;
- int pb = 1;
- int pt = 10;
-
- //:: if help requested or arguments lacking.
- if( !strcmp( argv[1], "/?" ) || ( argc == 1 ) )
- help();
- //:: if duplex specified, pop space, toggle.
- if( argv[2][0] == '=' )
- pd = argv[2][0] = 32;
- //:: if valid pointsize specified, change typeface size from 10.
- if( ( argc > 2 ) && atoi( argv[2] ) )
- pt = atoi( argv[2] );
- //:: if valid starting page specified, change beginning page from 1.
- if( ( argc > 3 ) && atoi( argv[3] ) )
- pb = atoi( argv[3] );
- //:: check status of LPT1.
- if( ( rc = printOK() ) != 144 )
- bail( "printer not captured?", rc );
- //:: write header. should return nothing.
- if( ( rc = writePP( argv[1], pt, pd, pb ) ) != 0 )
- bail( "ps header failure?", rc );
- //:: write file. should return nothing.
- if( ( rc = writePS( argv[1], pt ) ) != 0 )
- bail( "text print failure?", rc );
- //:: write trailer. should return nothing.
- if( ( rc = writePT( pd ) ) != 0 )
- bail( "print trailer failure?", rc );
- //:: leave stadium.
- return( 0 );
- }
-
- int printOK( void )
- {
- int rc;
- //:: couldn't figure actual decoding of biosprint().
- //:: apparent returns: 144=OK, 104=out of paper, 72=offline, 1=inactive.
- rc = biosprint( 2, '\0', 0 );
- return( rc );
- }
-
- void help( void )
- {
- //:: simple help. duplexing request a secret option.
- printf( "%s\narguments: [filename] [pointsize] [pagestart]\n", VERSION );
- printf( "(c) 1994 reece novelties" );
- exit( 1 );
- }
-
- void bail( char *cMessage, int rc )
- {
- //:: yer error handler.
- printf( "\n%s: %s\nreturn code: %d\n", VERSION, cMessage, rc );
- exit( 1 );
- }
-
- void padBS( char *s )
- {
- char cFile[128];
- int x = 0;
- int i = 0;
- int t = 0;
- x = strlen( s );
- for( i = 0; i < x; i ++ )
- {
- if ( s[i] == 92 )
- {
- cFile[t] = s[i];
- t++;
- }
- cFile[t] = s[i];
- t++;
- }
- cFile[t] = '\0';
- strcpy( s, cFile );
- }
-
- int writePP( char *filename, int pt, int pd, int pb )
- {
- time_t timer;
- struct tm *t;
- char cDate[32];
- char cFile[129];
- FILE *lpt;
-
- //:: copy filename, fix backslashes.
- strcpy( cFile, filename );
- padBS( cFile );
- //:: open printer stream.
- lpt = fopen( "PRN", "wb" );
- //:: thrash around and extract current time.
- timer = time( NULL );
- t = localtime( &timer );
- //:: store date string.
- sprintf( cDate, "%02d-%02d-%02d %02d:%02d:%02d",
- t->tm_mon + 1, t->tm_mday, t->tm_year,
- t->tm_hour, t->tm_min, t->tm_sec );
- //:: begin header:
- //:: end any previous jobs with ctrl-d.
- fprintf( lpt, "%c\n", 4 );
- fprintf( lpt, "%%!PS-Adobe-2.0 EPSF-1.2\n" );
- fprintf( lpt, "%%%%BoundingBox: 0 0 612 792\n" );
- fprintf( lpt, "%%%%Title: %s\n", cFile );
- fprintf( lpt, "%%%%Creator: PS.C\n" );
- fprintf( lpt, "%%%%CreationDate: %s\n", cDate );
- fprintf( lpt, "%%%%EndComments\n" );
- //:: if duplexing requested, presume languagelevel 2 printer ability.
- if( pd )
- {
- fprintf( lpt, "%%%%BeginSetup\n" );
- fprintf( lpt, "%%%%BeginFeature: *Duplex\n" );
- fprintf( lpt, "<< /Duplex true >> setpagedevice\n" );
- fprintf( lpt, "%%%%EndFeature\n" );
- fprintf( lpt, "%%%%EndSetup\n" );
- }
- fprintf( lpt, "%%%%BeginProlog\n" );
- fprintf( lpt, "/str 3 string def\n" );
- fprintf( lpt, "/pagenumber\n" );
- fprintf( lpt, "{\n" );
- //:: change to bold for page number info. if zero, skip
- fprintf( lpt, " /Courier-Bold findfont 10 scalefont setfont\n" );
- fprintf( lpt, " 72 750 moveto (%s) show\n", cFile );
- fprintf( lpt, " p 0 ne\n" );
- fprintf( lpt, " {\n" );
- fprintf( lpt, " 499 750 moveto (page ) show\n" );
- fprintf( lpt, " p str cvs show\n" );
- fprintf( lpt, " } if\n" );
- //:: box 612x792 page.
- fprintf( lpt, " .1 setlinewidth\n" );
- fprintf( lpt, " 72 740 moveto 468 0 rlineto stroke\n" );
- fprintf( lpt, " 48 24 moveto\n" );
- fprintf( lpt, " 516 0 rlineto 0 742 rlineto -516 0 rlineto\n" );
- fprintf( lpt, " closepath stroke\n" );
- //:: show puny time stamp.
- fprintf( lpt, " /Courier findfont 4 scalefont setfont\n", pt );
- fprintf( lpt, " 4 setlinewidth\n" );
- fprintf( lpt, " 1 setgray\n" );
- fprintf( lpt, " 284 23 moveto 43 0 rlineto stroke\n" );
- fprintf( lpt, " 285 23 moveto\n" );
- fprintf( lpt, " 0 setgray\n" );
- fprintf( lpt, " (%s) show\n", cDate );
- //:: restore pointsize.
- fprintf( lpt, " /Courier findfont %d scalefont setfont\n", pt );
- fprintf( lpt, "} bind def\n" );
- fprintf( lpt, "/printloop\n" );
- fprintf( lpt, "{\n" );
- //:: define beginning page number.
- fprintf( lpt, " /p %d def\n", pb );
- fprintf( lpt, " {\n" );
- fprintf( lpt, " currentfile cvlit =string readline not\n" );
- fprintf( lpt, " {\n" );
- fprintf( lpt, " exit\n" );
- fprintf( lpt, " } if\n" );
- fprintf( lpt, " show currentpoint exch\n" );
- //:: invoke leading as pt + 1.
- fprintf( lpt, " pop %d sub dup 36 le\n", pt + 1 );
- fprintf( lpt, " {\n" );
- fprintf( lpt, " pop\n" );
- fprintf( lpt, " p pagenumber\n" );
- fprintf( lpt, " /p p 1 add def\n" );
- fprintf( lpt, " pop showpage\n" );
- fprintf( lpt, " 72 %d moveto\n", 730 - pt );
- fprintf( lpt, " }\n" );
- fprintf( lpt, " {\n" );
- fprintf( lpt, " 72 exch moveto\n" );
- fprintf( lpt, " } ifelse\n" );
- fprintf( lpt, " } loop\n" );
- fprintf( lpt, " p 1 eq\n" );
- fprintf( lpt, " {\n" );
- fprintf( lpt, " /p 0 def\n" );
- fprintf( lpt, " } if\n" );
- fprintf( lpt, " p pagenumber\n" );
- fprintf( lpt, " showpage\n" );
- fprintf( lpt, "} bind def\n" );
- //:: set typeface to pt-point courier.
- fprintf( lpt, "/Courier findfont %d scalefont setfont\n", pt );
- fprintf( lpt, "72 %d moveto\n", 730 - pt );
- //:: end header:
- fprintf( lpt, "%%%%EndProlog\n" );
- fprintf( lpt, "printloop\n" );
- //:: DO NOT close lpt.
- // NO! fclose( lpt );
- //:: return nothing.
- return( 0 );
- }
-
-
- int writePS( char *filename, int pt )
- {
- int cc;
- char c;
- FILE *p;
- FILE *lpt;
-
- //:: open printer stream.
- lpt = fopen( "PRN", "wb" );
- //:: if file can't be opened, complain and leave.
- if( ( p = fopen( filename, "rb" ) ) == NULL )
- bail( "file can't be opened?", -1 );
- //:: open p and feed c to LPT1:
- c = fgetc( p );
- cc = 1;
- while( !feof( p ) )
- {
- //:: if it's a tab, do three spaces.
- if( c == 9 )
- {
- fprintf( lpt, " " );
- cc = cc + 2;
- }
- //:: if CR or two wide, whack.
- if( ( c == 13 )
- || ( cc > 127 )
- || ( cc * pt > 800 ) )
- {
- fprintf( lpt, "\n" );
- cc = 0;
- }
- //:: blow character out LPT port.
- if( isprint( c ) )
- fputc( c, lpt );
- c = fgetc( p );
- cc++;
- }
- //:: close job with ctrl-d.
- fprintf( lpt, "\n%%%%EOF%c", 4 );
- //:: close source file.
- fclose( p );
- fclose( lpt );
- //:: return nothing.
- return( 0 );
- }
-
- int writePT( int pd )
- {
- FILE *lpt;
-
- //:: if duplexing requested, turn off. presume languagelevel 2.
- //:: submit trailer job to turn duplexing off.
- if( pd )
- {
- //:: open printer stream.
- lpt = fopen( "PRN", "wb" );
- fprintf( lpt, "%c\n", 4 );
- fprintf( lpt, "%%!PS-Adobe-2.0 EPSF-1.2\n" );
- fprintf( lpt, "%%%%BoundingBox: 0 0 612 792\n" );
- fprintf( lpt, "%%%%Title: EndDuplex\n" );
- fprintf( lpt, "%%%%Creator: PS.C\n" );
- fprintf( lpt, "%%%%EndComments\n" );
- fprintf( lpt, "%%%%BeginSetup\n" );
- fprintf( lpt, "%%%%BeginFeature: *Duplex\n" );
- fprintf( lpt, "<< /Duplex false >> setpagedevice\n" );
- fprintf( lpt, "%%%%EndFeature\n" );
- fprintf( lpt, "%%%%EndSetup\n" );
- fprintf( lpt, "\n%%%%EOF%c", 4 );
- fclose( lpt );
- }
- //:: return nothing.
- return( 0 );
- }
-